package com.hjimi.ski.view.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by Administrator on 2019/1/31 0031.
 */

public class AlertController {
    private BaseDialog mBaseDialog;
    private Window mWindow;
    private DialogViewHelper mViewHelper;

    public AlertController(BaseDialog baseDialog, Window window){
        this.mBaseDialog = baseDialog;
        this.mWindow = window;
    }

    public void setViewHelper(DialogViewHelper viewHelper){
        this.mViewHelper = viewHelper;
    }

    /**
     * 设置文本
     * @param viewId
     * @param charSequence
     */
    public void setText(int viewId, CharSequence charSequence) {
        mViewHelper.setText(viewId,charSequence);
    }

    /**
     * 设置点击事件
     * @param viewId
     * @param listener
     */
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        mViewHelper.setOnClickListener(viewId,listener);
    }

    public <T extends View> T getView(int viewId) {
        return mViewHelper.getView(viewId);
    }

    /**
     * 获取Dialog
     * @return
     */
    public BaseDialog getDialog() {
        return mBaseDialog;
    }

    /**
     * 获取Dialog的Window
     * @return
     */
    public Window getWindow() {
        return mWindow;
    }

    public static class AlertParams{
        public Context mContext;
        public int mThemeResId;
        //点击空白是否能够取消 默认可以取消
        public boolean mCancelable = true;
        //dialog Cance监听
        public DialogInterface.OnCancelListener mOnCancelListener;
        //dialog Dismiss监听
        public DialogInterface.OnDismissListener mOnDismissListener;
        //dialog Key监听
        public DialogInterface.OnKeyListener mOnKeyListener;
        //布局的View
        public View mView;
        //布局的layouyId
        public int mViewLayoutResId;
        //用来存放一些字体的修改 （SparseArray可以代替HashMap更高效  前提是HashMap键值对Key是一个Integer）
        public SparseArray<CharSequence> mTextArray = new SparseArray<>();
        //存放点击事件
        public SparseArray<View.OnClickListener> mClickArray = new SparseArray();
        //宽度
        public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        //动画
        public int mAnimation = 0;
        //位置
        public int mGravity = Gravity.CENTER;
        //高度
        public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;

        public AlertParams(Context context, int themeResId) {
            this.mContext = context;
            this.mThemeResId = themeResId;
        }
        /**
         * 绑定和设置参数
         * @param mAlert
         */
        public void apply(AlertController mAlert){
            //1.设置Dialog布局 DialogHelper
            DialogViewHelper viewHelper = null;
            if(mViewLayoutResId != 0){
                viewHelper = new DialogViewHelper(mContext,mViewLayoutResId);
            }
            if(mView != null){
                viewHelper = new DialogViewHelper();
                viewHelper.setContentView(mView);
            }
            if(viewHelper == null){
                throw new IllegalArgumentException("请设置布局setContentView()");
            }

            //给Dialog 设置布局
            mAlert.getDialog().setContentView(viewHelper.getContentView());
            //设置Controller的辅助类
            mAlert.setViewHelper(viewHelper);

            //2.设置文本
            int textArraySize = mTextArray.size();
            for(int i=0;i<textArraySize;i++){
                mAlert.setText(i,mTextArray.get(i));
            }

            //3.设置点击
            int clickArraySize = mClickArray.size();
            for(int i=0;i<clickArraySize;i++){
                mAlert.setOnClickListener(i,mClickArray.get(i));
            }

            //4.配置自定义的效果 全屏 从底部弹出 默认动画
            Window window = mAlert.getWindow();
            //设置位置
            window.setGravity(mGravity);
            //设置动画
            if(mAnimation!=0){
                window.setWindowAnimations(mAnimation);
            }
            //设置宽高
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = mWidth;
            params.height = mHeight;
            window.setAttributes(params);
        }
    }
}
